package com.zygh.interactiveplatform.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.zygh.interactiveplatform.common.domain.ResponseDTO;
import com.zygh.interactiveplatform.module.entity.*;
import com.zygh.interactiveplatform.module.mapper.*;
import com.zygh.interactiveplatform.module.service.V1RestrictionAlgorithmServiceImpl;
import net.sf.json.JSONArray;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class InvalidTask {

    @Resource
    private CarstatisticsMapper carstatisticsMapper;
    @Resource
    private DataAccessMapper dataAccessMapper;
    @Resource
    private DataAccessTopsMapper dataAccessTopsMapper;
    @Resource
    private DataStructureMapper dataStructureMapper;
    @Resource
    private StorageTrendsMapper storageTrendsMapper;
    @Resource
    private DataInteractionTrendMapper dataInteractionTrendMapper;
    @Resource
    private DataAccessTop10Mapper dataAccessTop10Mapper;
    @Resource
    private LogTrendMapper logTrendMapper;
    @Resource
    private V1RestrictionAlgorithmServiceImpl v1RestrictionAlgorithmService;

    /**
     * 车辆实时统计定时统计插入数据库
     */

    @Scheduled(cron = "0 */30 * * * ?")
    public void InvalidData() {
        String URL = "http://192.168.10.130:8888/home/index";
        String res = HttpUtil.get(URL);
        JSONObject result = JSONObject.parseObject(res);
        Object total = result.getJSONObject("data").get("total");
        Object epolice1208 = result.getJSONObject("data").get("epolice1208");
        //Object epolice1244 = result.getJSONObject("data").get("epolice1244");
        Carstatistics carstatistics = new Carstatistics();
        carstatistics.setTotal(total.toString());
        carstatistics.setEpolice1208(epolice1208.toString());
        ResponseDTO responseDTO = v1RestrictionAlgorithmService.illegalVehicle();
        carstatistics.setEpolice1244(responseDTO.getData().toString());
        carstatistics.setCreattime(DateUtil.now());
        carstatisticsMapper.insert(carstatistics);
        System.out.println("车辆实时统计定时统计插入数据库");

    }

    /**
     * 数据接入情况
     */


    //@Scheduled(cron = "0/30 * *  * * ? ")
    @Scheduled(cron = "0 */40 * * * ?")
    public void epolice6011() {
        String URL = "http://192.168.10.130:8888/home/epolice6011";
        String res = HttpUtil.get(URL);
        JSONObject result = JSONObject.parseObject(res);
        Object epolice6011 = result.getJSONObject("data").get("epolice6011");
        Object epolice3019 = result.getJSONObject("data").get("epolice3019");
        Object epolice13451 = result.getJSONObject("data").get("epolice13451");
        Object epolice1625 = result.getJSONObject("data").get("epolice1625");
        DataAccess dataAccess = new DataAccess();
        dataAccess.setCreattime(DateUtil.now());
        dataAccess.setEpolice6011(epolice6011.toString());
        dataAccess.setEpolice3019(epolice3019.toString());
        dataAccess.setEpolice13451(epolice13451.toString());
        dataAccess.setEpolice1625(epolice1625.toString());
        dataAccessMapper.insert(dataAccess);
        System.out.println("数据接入情况定时统计插入成功");
    }

    /**
     * 数据接入tops
     */
    @Scheduled(cron = "0 */50 * * * ?")
    public void epoliceOne() {
        String URL = "http://192.168.10.130:8888/home/epoliceOne";
        String res = HttpUtil.get(URL);
        JSONObject result = JSONObject.parseObject(res);
        Object epolice1625 = result.getJSONObject("data").get("epolice1625");
        Object epoliceTwo = result.getJSONObject("data").get("epoliceTwo");
        Object epolice1301 = result.getJSONObject("data").get("epolice1301");
        Object epolice1344 = result.getJSONObject("data").get("epolice1344");
        Object epoliceOne = result.getJSONObject("data").get("epoliceOne");
        DataAccessTops dataAccessTops = new DataAccessTops();
        dataAccessTops.setCreattime(DateUtil.now());
        dataAccessTops.setEpolice1625(epolice1625.toString());
        dataAccessTops.setEpoliceTwo(epoliceTwo.toString());
        dataAccessTops.setEpolice1301(epolice1301.toString());
        dataAccessTops.setEpolice1344(epolice1344.toString());
        dataAccessTops.setEpoliceOne(epoliceOne.toString());
        dataAccessTopsMapper.insert(dataAccessTops);
        System.out.println("数据接入tops情况定时统计插入成功!");
    }


    /**
     * 数据结构分类
     */
    @Scheduled(cron = "0 */20 * * * ?")
    public void epoliceTwo() {
        String URL = "http://192.168.10.130:8888/home/per/struts";
        String res = HttpUtil.get(URL);
        JSONObject result = JSONObject.parseObject(res);
        Object txt = result.getJSONObject("data").get("txt");
        Object link = result.getJSONObject("data").get("link");
        Object pic = result.getJSONObject("data").get("pic");
        Object video = result.getJSONObject("data").get("video");
        DataStructure dataStructure = new DataStructure();
        dataStructure.setCreattime(DateUtil.now());
        dataStructure.setTxt(txt.toString());
        dataStructure.setLink(link.toString());
        dataStructure.setPic(pic.toString());
        dataStructure.setVideo(video.toString());
        dataStructureMapper.insert(dataStructure);
        System.out.println("数据结构分类定时插入成功!");
    }

    /**
     * 统计每小时数据
     */
    //@Scheduled(cron = "0/60 * *  * * ? ")
    @Scheduled(cron = "0 */60 * * * ?")
    public void hour() {
        String URL = "http://192.168.10.130:8888/home/per/hour";
        String res = HttpUtil.get(URL);
        net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(res);
        JSONArray biz_result = jsonObject.getJSONArray("data");
        StorageTrends storageTrends = new StorageTrends();
        //获取数组中的某位元素
        for (int i = 0; i < biz_result.size(); i++) {
            String biz_resultString = biz_result.getString(i);
            net.sf.json.JSONObject jsonObject1 = net.sf.json.JSONObject.fromObject(biz_resultString);
            String hours = jsonObject1.getString("hours");
            String count = jsonObject1.getString("count");
            storageTrends.setCreattime(DateUtil.now());
            storageTrends.setHours(hours);
            storageTrends.setCount(count);
            storageTrendsMapper.insertIgnoreNull(storageTrends);
        }
        System.out.println("统计每小时数据定时插入成功!");
    }

    /**
     * 数据交互趋势
     */

    @Scheduled(cron = "0 */20 * * * ?")
    //@Scheduled(cron = "0/10 * *  * * ? ")
    public void dataTrend() {
        String URL = "http://192.168.10.130:8888/first-filter/line";
        String res = HttpUtil.get(URL);
        net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(res);
        net.sf.json.JSONObject data = jsonObject.getJSONObject("data");
        JSONArray list = data.getJSONArray("listOne");
        JSONArray listTwo = data.getJSONArray("listTwo");

        DataInteractionTrend dataInteractionTrend = new DataInteractionTrend();
        //获取数组中的某位元素
        for (int i = 0; i < list.size(); i++) {
            String biz_resultString = list.getString(i);
            net.sf.json.JSONObject jsonObject2 = net.sf.json.JSONObject.fromObject(biz_resultString);
            String hours = jsonObject2.getString("hours");
            String count = jsonObject2.getString("count");
            dataInteractionTrend.setCreattime(DateUtil.now());
            dataInteractionTrend.setHours(hours);
            dataInteractionTrend.setCount(count);
            dataInteractionTrendMapper.updateNonredundant(dataInteractionTrend);
        }
        /**
         * 违法数据
         */
        ResponseDTO responseDTO = v1RestrictionAlgorithmService.illegalVehicle();
        Object data1 = responseDTO.getData();
        String now = DateUtil.now();
        String substring = now.substring(now.indexOf(" ") + 1, now.indexOf(":"));
        dataInteractionTrend.setCreattime(DateUtil.now());
        dataInteractionTrend.setHours(substring);
        dataInteractionTrend.setCount2(data1.toString());
        dataInteractionTrendMapper.updateDataInteractionTrend(dataInteractionTrend);
        System.out.println("数据交互趋势");
    }

    /**
     * 数据接入top10
     */
    //@Scheduled(cron = "0/10 * *  * * ? ")
    @Scheduled(cron = "0 */30 * * * ?")
    public void dataAccessTopjr() {
        String URL = "http://192.168.10.130:8888/first-filter/top";
        String s = HttpUtil.get(URL);
        net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(s);
        JSONArray biz_result = jsonObject.getJSONArray("data");
        String string1 = biz_result.getString(0);
        net.sf.json.JSONObject jsonObject2 = net.sf.json.JSONObject.fromObject(string1);
        Object epolice1625 = jsonObject2.getString("epolice1625");
        Object epolice6011 = jsonObject2.getString("epolice6011");
        Object epolice3019 = jsonObject2.getString("epolice3019");
        Object epolice1208 = jsonObject2.getString("epolice1208");
        Object one = jsonObject2.getString("one");
        Object epolice13451 = jsonObject2.getString("epolice13451");
        Object epolice1301 = jsonObject2.getString("epolice1301");
        Object two = jsonObject2.getString("two");
        Object epolice1344 = jsonObject2.getString("epolice1344");
        Object epolice1244 = jsonObject2.getString("epolice1244");
        DataAccessTop10 dataAccessTop10 = new DataAccessTop10();
        dataAccessTop10.setCreattime(DateUtil.now());
        dataAccessTop10.setEpolice1625(epolice1625.toString());
        dataAccessTop10.setEpolice6011(epolice6011.toString());
        dataAccessTop10.setEpolice3019(epolice3019.toString());
        dataAccessTop10.setEpolice1208(epolice1208.toString());
        dataAccessTop10.setOne(one.toString());
        dataAccessTop10.setEpolice13451(epolice13451.toString());
        dataAccessTop10.setEpolice1301(epolice1301.toString());
        dataAccessTop10.setTwo(two.toString());
        dataAccessTop10.setEpolice1344(epolice1344.toString());
        dataAccessTop10.setEpolice1244(epolice1244.toString());
        dataAccessTop10Mapper.insert(dataAccessTop10);
        System.out.println("数据接入top10!");
    }

    /**
     * 日志采集趋势
     */
    //@Scheduled(cron = "0/10 * *  * * ? ") //秒
    @Scheduled(cron = "0 */40 * * * ?") //分钟
    public void logTrends() {
        String URL = "http://192.168.10.130:8888/first-filter/log";
        String res = HttpUtil.get(URL);
        net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(res);
        JSONArray biz_result = jsonObject.getJSONArray("data");
        LogTrend logTrend = new LogTrend();
        //获取数组中的某位元素
        for (int i = 0; i < biz_result.size(); i++) {
            String biz_resultString = biz_result.getString(i);
            net.sf.json.JSONObject jsonObject1 = net.sf.json.JSONObject.fromObject(biz_resultString);
            String hours = jsonObject1.getString("hours");
            String count = jsonObject1.getString("count");
            logTrend.setCreattime(DateUtil.now());
            logTrend.setHours(hours);
            logTrend.setCount(count);
            logTrendMapper.insertLogTrend(logTrend);
        }
        System.out.println("日志采集趋势定时插入成功!");
    }


}
